﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using kim.Common;

namespace kim.Web.Admin.honor
{
    public partial class AddorEdit : System.Web.UI.Page
    {
        protected string type;
        protected int typeid;
        private int Nid;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = KIMRequest.GetString("type");
            Nid = KIMRequest.GetInt("id", 1);
            if (!IsPostBack)
                GetRequestValues();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }

        }
        #region 编辑添加基本操作=========================================

        private bool DoEdit(int id)
        {
            Model.rencai Mnew = new Model.rencai();
            Mnew.InTime = DateTime.Now;
            Mnew.Numbers = txt_num.Value.Trim();
            Mnew.Content = txt_Content.Value;
            Mnew.ClassID = typeid;
            if (CbIsShow.Checked)
                Mnew.IsShow = 1;
            else
                Mnew.IsShow = 0;
            Mnew.Name = txtTitle.Value;
            Mnew.Gender = txtgender.Value;
            Mnew.Tel = txtTel.Value;
            Mnew.Salary = txtSalary.Value;
            Mnew.Workarea = txtWorkarea.Value;
            Mnew.Origin = txtOrigin.Value;
            Mnew.ID = id;
            return new BLL.rencai().Update(Mnew);
        }

        private bool DoAdd()
        {
            Model.rencai Mnew = new Model.rencai();
            Mnew.InTime = DateTime.Now;
            Mnew.Numbers = txt_num.Value.Trim();
            Mnew.Content = txt_Content.Value;
            Mnew.ClassID = typeid;
            if (CbIsShow.Checked)
                Mnew.IsShow = 1;
            else
                Mnew.IsShow = 0;
            Mnew.Name = txtTitle.Value;
            Mnew.Gender = txtgender.Value;
            Mnew.Tel = txtTel.Value;
            Mnew.Salary = txtSalary.Value;
            Mnew.Workarea = txtWorkarea.Value;
            Mnew.Origin = txtOrigin.Value;
            BLL.rencai bll = new BLL.rencai();
            if (bll.Add(Mnew) > 0)
                return true;
            else
                return false;
        }
        #endregion

        private void GetRequestValues()
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.rencai().Delete(Nid);
                if (del)
                    Response.Redirect("List.aspx");
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
        }

        private void GetEditValue()
        {

            Model.rencai model = new BLL.rencai().GetModel(Nid);
            this.txtTitle.Value = model.Name;
            this.txt_num.Value = model.Numbers.ToString();
            txtOrigin.Value = model.Origin;
            txt_Content.Value = model.Content;
            txtTitle.Value = model.Name;
            txtTel.Value = model.Tel;
            if (model.IsShow == 1)
                CbIsShow.Checked = true;
            else
                CbIsShow.Checked = false;
            txtgender.Value = model.Gender;
            txtSalary.Value = model.Salary;
            txt_Content.Value = model.Content;
            txtWorkarea.Value = model.Workarea;
            txtOrigin.Value = model.Origin;
        }
        protected void htn_upload_Click(object sender, EventArgs e)//上传
        {
            //string strpic = PubFileAndImageFunc.fileuploadcode(this.FileUpload1, "../../pic/");
            //ViewState["img"] = strpic != "" ? strpic : this.Headpic.Src.ToString();
            //this.Headpic.Src = ViewState["img"].ToString();
        }
    }
}